#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2025/2/19 18:36
# @Author    :jiaoxianzhong
# @FileName  :selenium-remote-debugging.py

import base64
import os
import random
import time
from io import BytesIO

import requests
from PIL.Image import Image
from fake_useragent import UserAgent
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import ddddocr

# 启动浏览器命令
# start D:/Code/Python/flask-project/chrome-win/chrome.exe --remote-debugging-port=13888

# 实例化 UserAgent 类
ua = UserAgent()

# 当前文件所在的文件夹路径
current_path = os.path.abspath(os.path.dirname(__file__))
# chrome浏览器路径
chrome_location = os.path.join(current_path, 'chrome-win')
# chrome.exe完整路径
browser_location = os.path.join(chrome_location, 'chrome.exe')
# chromedriver.exe的完整路径
driver_location = os.path.join(chrome_location, 'chromedriver.exe')
# 创建一个Servic对象，传入ChromeDriver的路径
service = Service(driver_location)
# 创建Chrome选项
options = Options()
# options.binary_location = browser_location
# # 不显示 Chrom正受到自动测试软件控制
# options.add_experimental_option("excludeSwitches", ['enable-automation'])
# 禁用自动化控制特性，避免被网站检测到
# window.navigator.webdriver属性设置为: false
# options.add_argument("--disable-blink-features=AutomationControlled")
# 无头模式
# options.add_argument("--headless")
# options.add_argument(f'User-Agent={ua.random}')
options.add_argument(f'--user-agent={ua.random}')
# options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36')
# 关闭webrtc
# preferences = {
#     "webrtc.ip_handling_policy": "disable_non_proxied_udp",
#     "webrtc.multiple_routes_enabled": False,
#     "webrtc.nonproxied_udp_enabled": False
# }
# options.add_experimental_option("prefs", preferences)
# options.add_argument('--start-maximized')
# options.add_argument('--remote-debugging-port=13888')

options.add_experimental_option("debuggerAddress", "127.0.0.1:13888")

driver = webdriver.Chrome(service=service, options=options)
# driver = webdriver.Remote(command_executor="http://127.0.0.1:13888",options=options)
# driver = webdriver.Chrome(options=options)
det = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)
wait = WebDriverWait(driver, 20, poll_frequency=0.5)


if __name__ == '__main__':
    try:
        # url = "https://www.etrading.cn/captcha/c?url=https://www.etrading.cn/noticeDetails_jyxx.html?infoid=f063503f-d425-4c0c-85a2-fbe57d244a26&categorynum=002001001&title=%E5%A6%82%E7%9A%8B%E5%B8%82%E9%95%BF%E6%B1%9F%E9%95%87%E5%AE%9C%E5%B1%85%E5%AE%9C%E4%B8%9A%E5%92%8C%E7%BE%8E%E4%B9%A1%E6%9D%91%E5%BB%BA%E8%AE%BE%E9%A1%B9%E7%9B%AE(%E6%9D%A8%E6%B4%B2%E7%A4%BE%E5%8C%BA)?"
        # url="https://baijiahao.baidu.com/s?id=1816228266652249286"
        # url="https://www.ccgp-gansu.gov.cn/web/article/8aeeb5df93241991019347503d933724.html"
        url="https://www.creditchina.gov.cn"
        # url="https://nowsecure.nl"
        driver.get(url)
        driver.quit()
        # 是否需要验证
        with open(os.path.join(current_path,"js","Cursor.js"),'r',encoding="utf-8") as f:
            mouse_js=f.read()
            driver.execute_script(mouse_js)
    except Exception as e:
        print(e)
    finally:
        wait_result=driver.find_element(By.XPATH, '//*[@id="kz002001001"]').text
        print(wait_result)
        driver.quit()
